<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class purchase_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$data=array();

	} 	
		

	

	public function get_purchaseList(){
		$this->db->select('purchase.*, users.username as supplier_name');
		$this->db->from('purchase','users' );
		$this->db->join('users', 'purchase.id_supplier= users.id'  );
		$result = $this->db->get();
		return $result;
	} 
	


public function get_product($id){
	$query = $this->db->get_where('product', array('code' => $id));
	return $query;

	}

public function get_invoicecode($id_branch){
	
	$query = $this->db->get_where('branch', array('id' => $id_branch));	
	return $query;
}


	public function get_branch(){
			$q = $this->db->get('branch');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
				
			return $data;
		}
	}

	
	public function get_tax(){
	$q = $this->db->get('tax');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
				
			return $data;
		}
		
	}	
	
public function  get_customer(){
		$this->db->select('supplier.*');
	$this->db->from('supplier');
	//$this->db->join('company', 'product.id_company= company.id');
	$q = $this->db->get();
		//$q = $this->db->get_where('customer',array(''=>'');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
				
			return $data;
		}
	}	

	





public function get_profile(){
		
	$query = $this->db->get('company_profile');

	return $query;
}	


/*public function get_groups(){
	$this->db->select('acc_group.*,projects.name');
		$this->db->from('tasks');
		$this->db->where('tasks.id ='.$id);
		$this->db->join('projects','tasks.project_id=projects.id');
		return $this->db->get()->row_array();
		$query = $this->db->get('acc_group');
	return $query;
}
*/

public function get_invoiceno(){
	
	$this->db->from('sales');
	$this->db->order_by("id", "asc");
	$this->db->limit(1);
	$query = $this->db->get(); 
	return $query;

}


public function insert($data){

		//print_r($data);exit;
		if($data['transaction_type']==0){
			$data['transaction_type'] = 'Cash';
			$this->db->insert('purchase',$data);
			$latest_id = $this->db->insert_id();
			$data1 = array(
			'id_user' => $data['id_supplier'],
			'user_name' => $data['supplier_name'],
			'voucher_type' => 'purchase',
			'payment_type' => 0,
			'payment_mode' => 'Cash',
			'voucher_no' => $data['referance_no'],
			'date' => $data['referance_date'],
			'debit_amount' => $data['total_price'],
			'credit_amount' => $data['total_price'],
			'type' => 2,
			'ip' => $_SERVER['REMOTE_ADDR']
			);
		//	print_r($data1);exit;
		$this->db->insert('accounts',$data1);
		}else{
			$data['transaction_type'] = 'Credit';
			///print_r($data);exit;
			$this->db->insert('purchase',$data);
			$latest_id = $this->db->insert_id();
			$data1 = array(
			'id_user' => $data['id_supplier'],
			'user_name' => $data['supplier_name'],
			'voucher_type' => 'purchase',
			'payment_type' => 0,
			'payment_mode' => '',
			'voucher_no' => $data['referance_no'],
			'date' => $data['referance_date'],
			'credit_amount' => $data['total_price'],
			'type' => 2,
			'ip' => $_SERVER['REMOTE_ADDR']
			);
			$this->db->insert('accounts',$data1);
		}
	return $latest_id;
}
	
public function update($data,$id){
 
		if($data['transaction_type']==0){
			$data['transaction_type'] = 'Cash';
			$user_id=$data['id_supplier'];
			$voucher_no=$data['referance_no'];
			$this->db->update('purchase',$data,array("id"=> $id));
			$data1 = array(
			'id_user' => $data['id_supplier'],
			'user_name' => $data['supplier_name'],
			'voucher_type' => 'purchase',
			'payment_type' => 0,
			'payment_mode' => 'Cash',
			'voucher_no' => $data['referance_no'],
			'date' => $data['referance_date'],
			'debit_amount' => $data['total_price'],
			'credit_amount' => $data['total_price'],
			'type' => 2,
			'ip' => $_SERVER['REMOTE_ADDR']
			);
		
		 $this->db->update('accounts',$data1,array("voucher_no"=>$voucher_no )); 
		}else{
			$data['transaction_type'] = 'Credit';
			$this->db->update('purchase',$data,array("id"=> $id));
			$user_id=$data['id_supplier'];
			$voucher_no=$data['referance_no'];
			$data1 = array(
			'id_user' => $data['id_supplier'],
			'user_name' => $data['supplier_name'],
			'voucher_type' => 'purchase',
			'payment_type' => 0,
			'payment_mode' => '',
			'debit_amount' =>0,
			'voucher_no' => $data['referance_no'],
			'date' => $data['referance_date'],
			'credit_amount' => $data['total_price'],
			'type' => 2,
			'ip' => $_SERVER['REMOTE_ADDR']
			);
		 $this->db->update('accounts',$data1,array("voucher_no"=>$voucher_no ));
		}
		
		 $this->db->update('purchase',$data,array('id'=>$id)); 
		
		
} 

public function insert_item($data,$id_branch){ 
	if($id_branch == 0){ 
		$product = $this->db->get_where('product', array('id' => $data['id_product']))->row_array();	
		$new_stock = $product['opening_stock']+$data['quantity'];
		$warehouse_product_num = $this->db->get_where('warehouse', array('id_product' => $data['id_product']))->num_rows();	
		if($warehouse_product_num == 0){
			$data1 = array('id_product' => $data['id_product'], 
							'opening_stock' => $data['quantity']
						);
			$this->db->insert('warehouse',$data1);	
		}else{
			$branch_product = $this->db->get_where('warehouse', array('id_product' => $data['id_product']))->row_array();		
			$new_qty1 = $branch_product['opening_stock'] +$data['quantity'];
			$data2 = array(
				'opening_stock' => $new_qty1 
			);	
			$this->db->update('warehouse',$data2,array('id_product' => $data['id_product']));
		
		}
			
		$up_data = array('opening_stock'=>$new_stock);
		$this->db->update('product',$up_data,array('id' => $data['id_product']));
		//print_r($data);exit;
		$this->db->insert('purchase_product',$data);
	}else{
	
		$product = $this->db->get_where('product', array('id' => $data['id_product']))->row_array();	
		$branch_product_num = $this->db->get_where('branch_product', array('id_product' => $data['id_product'], 'id_branch' => $id_branch))->num_rows();	
		if($branch_product_num==0){
			$data1 = array('id_product' => $data['id_product'], 
							'id_branch' => $id_branch,
							'opening_stock' => $data['quantity']
						);
			$this->db->insert('branch_product',$data1);	
		}else{
			$branch_product = $this->db->get_where('branch_product', array('id_product' => $data['id_product'], 'id_branch' => $id_branch))->row_array();		
			$new_qty1 = $branch_product['opening_stock'] +$data['quantity'];
			$data2 = array(
				'opening_stock' => $new_qty1 
			);	
			$this->db->update('branch_product',$data2,array('id'=>$branch_product['id']));
		
		}
		$new_qty = $product['opening_stock']+$data['quantity'];
			$data3 = array(
			'opening_stock' => $new_qty 
			);	
			
		$this->db->update('product',$data3,array('id'=>$product['id']));
		//print_r($data);exit;

		$this->db->insert('purchase_product',$data);
		}
		
	}
	
	
	
	///////////////////////////
 public function update_item($data,$id_branch,$id_pur_product,$branch){ 
 $purchase= $this->db->get_where('purchase_product', array('id_product' => $data['id_product'],'id'=>$id_pur_product))->num_rows();
 if($purchase !=0)
 {
//print_r($data);exit;
 $product = $this->db->get_where('product', array('id' => $data['id_product']))->row_array();
$purchase_product=$this->db->get_where('purchase_product',array('id'=>$id_pur_product))->row_array(); 


 if($id_branch == 0){ 
 $warehouse_product_num = $this->db->get_where('warehouse', array('id_product' => $data['id_product']))->num_rows();
 $product = $this->db->get_where('product', array('id' => $data['id_product']))->row_array();
$purchase_product=$this->db->get_where('purchase_product',array('id'=>$id_pur_product))->row_array(); 
if($warehouse_product_num==0){
 if($id_branch==$branch)
 {
  $purchase_product=$this->db->get_where('purchase_product',array('id'=>$id_pur_product))->row_array();
 $stock_data=array('opening_stock'=>$data['quantity']);
 $this->db->insert('warehouse',$stock_data,array('id_product'=>$data['id_product']));
 $total_stock=intval($product['opening_stock'])+intval($data['quantity'])-intval($purchase_product['quantity']);
			$stock=array('opening_stock'=>$total_stock);
			$this->db->update('product',$stock,array('id'=>$data['id_product']));
		    }
	else{ 
	         $purchase_product=$this->db->get_where('purchase_product',array('id'=>$id_pur_product))->row_array();
           $stock_data=array('opening_stock'=>$data['quantity']);
            $this->db->insert('warehouse',$stock_data,array('id_product'=>$data['id_product']));
			$branchs=$this->db->get_where('branch_product', array('id_product' => $data['id_product'], 'id_branch' => $branch))->row_array();
			$new_branch=intval($branchs['opening_stock'])-intval($purchase_product['quantity']);
			$new=array('opening_stock'=>$new_branch);
		    $this->db->update('branch_product',$new,array('id_branch' => $id_branch,'id_product'=>$data['id_product']));
			$total_stock=intval($product['opening_stock'])+intval($data['quantity'])-intval($purchase_product['quantity']);
			$stock=array('opening_stock'=>$total_stock);
			$this->db->update('product',$stock,array('id'=>$data['id_product']));
		    }

}	
else{ 
  if($id_branch==$branch)
 {    $warehouse_stock=$this->db->get_where('warehouse',array('id_product'=>$data['id_product']))->row_array();
       $purchase_product=$this->db->get_where('purchase_product',array('id'=>$id_pur_product))->row_array(); 
        $data_ware=intval($warehouse_stock['opening_stock'])+intval($data['quantity'])-intval($purchase_product['quantity']);
        $stock_data=array('opening_stock'=>$data_ware);

        $this->db->update('warehouse',$stock_data,array('id_product'=>$data['id_product']));
        $total_stock=intval($product['opening_stock'])+intval($data['quantity'])-intval($purchase_product['quantity']);
			$stock=array('opening_stock'=>$total_stock);
			$this->db->update('product',$stock,array('id'=>$data['id_product']));
		    }
	else{  
	        $purchase_product=$this->db->get_where('purchase_product',array('id'=>$id_pur_product))->row_array();
	        $warehouse_stock=$this->db->get_where('warehouse',array('id_product'=>$data['id_product']))->row_array();
			$new_stock=intval($warehouse_stock['opening_stock'])+intval($data['quantity']);
            $stock_data=array('opening_stock'=>$new_stock);
            $this->db->update('warehouse',$stock_data,array('id_product'=>$data['id_product']));
			$branchs=$this->db->get_where('branch_product', array('id_product' => $data['id_product'], 'id_branch' => $branch))->row_array();
			 $data1=$branchs['opening_stock'];
			 $data2=$purchase_product['quantity']; 
			 $data3=$data1-$data2;
			
			//$new_branch=intval($branchs['opening_stock'])-intval($purchase_product['quantity']);
			$new=array('opening_stock'=>$data3);
		    $this->db->update('branch_product',$new,array('id_branch' => $branch,'id_product'=>$data['id_product']));
			$total_stock=intval($product['opening_stock'])+intval($data['quantity'])-intval($purchase_product['quantity']);
			$stock=array('opening_stock'=>$total_stock);
			$this->db->update('product',$stock,array('id'=>$data['id_product']));
		
		    }
 
 }
 }
 
 
 
 
 
 
 else{
 	if($id_branch==$branch){ 
 if($this->db->get_where('branch_product', array('id_product' => $data['id_product'], 'id_branch' => $id_branch))->num_rows() == 0){ 
  $purchase_product=$this->db->get_where('purchase_product',array('id'=>$id_pur_product))->row_array(); 
 $branchs=$this->db->get_where('branch_product', array('id_product' => $data['id_product'], 'id_branch' => $branch))->row_array();
 $new_branch=intval($branchs['opening_stock']) + intval($data['quantity'])-intval($purchase_product['quantity']);
 $new=array('opening_stock'=>$new_branch,
            'id_product'=>$data['id_product'],
			'id_branch'=>$id_branch
   );
 $this->db->insert('branch_product',$new);
 $total_stock=intval($product['opening_stock'])+intval($data['quantity'])-intval($purchase_product['quantity']);
 $stock=array('opening_stock'=>$total_stock);
 $this->db->update('product',$stock,array('id'=>$data['id_product']));
 }
 else{ 
 $branchs=$this->db->get_where('branch_product', array('id_product' => $data['id_product'], 'id_branch' => $branch))->row_array();
 $purchase_product=$this->db->get_where('purchase_product',array('id'=>$id_pur_product))->row_array(); 
 $new_branch=intval($branchs['opening_stock'])+intval($data['quantity'])-intval($purchase_product['quantity']);
 $new=array('opening_stock'=>$new_branch);
 $this->db->update('branch_product',$new,array('id_branch' => $id_branch,'id_product'=>$data['id_product']));
 $total_stock=intval($product['opening_stock'])+intval($data['quantity'])-intval($purchase_product['quantity']);
 $stock=array('opening_stock'=>$total_stock);
 $this->db->update('product',$stock,array('id'=>$data['id_product']));
  }
  }else{
  if($branch == 0 ){ 
	 if($num_branch=$this->db->get_where('branch_product', array('id_product' => $data['id_product'], 'id_branch' => $id_branch))->num_rows()==0){
	 $warehouse_stock=$this->db->get_where('warehouse',array('id_product'=>$data['id_product']))->row_array();
	 $new_warehouse=intval($warehouse_stock['opening_stock']) -intval($purchase_product['quantity']);
	 $ware=array('opening_stock'=>$new_warehouse);
    $this->db->update('warehouse',$ware,array('id_product'=>$data['id_product']));
    $branchs=$this->db->get_where('branch_product', array('id_product' => $data['id_product'], 'id_branch' => $id_branch))->row_array();
     $new_branch=intval($branchs['opening_stock'])+intval($data['quantity']);
     $new=array('opening_stock'=>$new_branch,
           'id_product'=>$data['id_product'],
			'id_branch'=>$id_branch
   );
   $this->db->insert('branch_product',$new);
   $total_stock=intval($product['opening_stock'])+intval($data['quantity'])-intval($purchase_product['quantity']);
   $stock=array('opening_stock'=>$total_stock);
  $this->db->update('product',$stock,array('id'=>$data['id_product']));
 
 } else{
 
 $warehouse_stock=$this->db->get_where('warehouse',array('id_product'=>$data['id_product']))->row_array();
 $new_warehouse=intval($warehouse_stock['opening_stock']) -intval($purchase_product['quantity']);
 $ware=array('opening_stock'=>$new_warehouse);
 $this->db->update('warehouse',$ware,array('id_product'=>$data['id_product']));
 $branchs=$this->db->get_where('branch_product', array('id_product' => $data['id_product'], 'id_branch' => $id_branch))->row_array();
 $new_branch=intval($branchs['opening_stock'])+intval($data['quantity']);
 $new=array('opening_stock'=>$new_branch
   );
 $this->db->update('branch_product',$new,array('id_branch'=>$id_branch,'id_product'=>$data['id_product']));
 $total_stock=intval($product['opening_stock'])+intval($data['quantity'])-intval($purchase_product['quantity']);
 $stock=array('opening_stock'=>$total_stock);
$this->db->update('product',$stock,array('id'=>$data['id_product']));
 }}
///// 

 else{ 


 if($this->db->get_where('branch_product', array('id_product' => $data['id_product'], 'id_branch' => $id_branch))->num_rows()==0)
 { 
 $current_branchs=$this->db->get_where('branch_product', array('id_product' => $data['id_product'], 'id_branch' => $id_branch))->row_array();
 $current_stock=intval($data['quantity']);
 $new_stock=array('opening_stock' =>$current_stock,
                  'id_product'=>$data['id_product'],
				  'id_branch' =>$id_branch
                     );
					 ;
 $this->db->insert('branch_product',$new_stock);
  $total_stock=intval($product['opening_stock'])+intval($data['quantity'])-intval($purchase_product['quantity']);
 $stock=array('opening_stock'=>$total_stock);
$this->db->update('product',$stock,array('id'=>$data['id_product']));
 $branchs=$this->db->get_where('branch_product', array('id_product' => $data['id_product'], 'id_branch' => $branch))->row_array();
 $new_branch=intval($branchs['opening_stock'])- intval($purchase_product['quantity']);
 $new=array('opening_stock'=>$new_branch);
 
 $this->db->update('branch_product',$new,array('id_branch' => $branch,'id_product'=>$data['id_product']));
 }
 else{  
 $current_branchs=$this->db->get_where('branch_product', array('id_product' => $data['id_product'], 'id_branch' => $id_branch))->row_array();
 $current_stock=intval($current_branchs['opening_stock'])+intval($data['quantity']);
 $new_stock=array('opening_stock' =>$current_stock);

 $this->db->update('branch_product',$new_stock,array('id_branch' => $id_branch ,'id_product'=>$data['id_product']));
 $total_stock=intval($product['opening_stock'])+intval($data['quantity'])-intval($purchase_product['quantity']);
 $stock=array('opening_stock'=>$total_stock);
 $this->db->update('product',$stock,array('id'=>$data['id_product']));
 $branchs=$this->db->get_where('branch_product', array('id_product' => $data['id_product'], 'id_branch' => $branch))->row_array();
 $data_item1=intval($branchs['opening_stock']);
 $data_item2=intval($purchase_product['quantity']);
 $new_branch=$data_item1-$data_item2;

 //$new_branch=intval($branchs['opening_stock']) - intval($data['quantity']);
 $new=array('opening_stock'=>$new_branch);
 $this->db->update('branch_product',$new,array('id_branch' => $branch,'id_product' =>$data['id_product']));
 }
 }
 
}
 

 } 
 
 $data5=array('id_purchase' =>$data['id_purchase'],
    
	 'id_product' =>$data['id_product'],
	 'quantity' =>$data['quantity'],
	 'rate' =>$data['rate'],
	 'amount' =>$data['amount'],
	 'tax' =>$data['tax'],
	 'discount' =>$data['discount'],
	
	 'total' =>$data['total'],
	 'date' =>$data['date'],
	 'ip' =>$data['ip'],
	  );

   $this->db->update('purchase_product',$data5,array('id' =>$id_pur_product)); 
 } 
 else{

 $purchase_product=$this->db->get_where("purchase_product",array( "id" => $id_pur_product, ))->row_array();
if(!empty($purchase_product))
{
 if($branch==0 AND $id_branch==0)
 {
  $warehouse1=$this->db->get_where('warehouse',array('id_product'=>$purchase_product['id_product']))->row_array();
   if(!empty( $warehouse1))
   {
   $warehouse_stock=intval($warehouse1['opening_stock'])-intval($purchase_product['quantity']);
    $insert1=array('opening_stock'=>$warehouse_stock,);
	 $this->db->update('warehouse',$insert1,array('id_product'=>$purchase_product['id_product'],));
	 } else{}
	 if($this->db->get_where('warehouse',array('id_product'=>$data['id_product']))->num_rows() !=0)
	 {
   $warehouse2=$this->db->get_where('warehouse',array('id_product'=>$data['id_product']))->row_array();
   $warehouse_stock2=intval($warehouse2['opening_stock'])+intval($data['quantity']);
  $insert2=array('opening_stock'=>$warehouse_stock2,);
  $this->db->update('warehouse',$insert2,array('id_product'=>$data['id_product'],));
  } else{ $new=array('id_product'=>$data['id_product'],
                     'opening_stock'=>$data['quantity'],);
					 $this->db->insert('warehouse',$new);
  }
  $product1=$this->db->get_where('product',array('id'=>$purchase_product['id_product']))->row_array();
  $product2=$this->db->get_where('product',array('id'=>$data['id_product']))->row_array();
  $product_stock1=intval($product1['opening_stock'])-intval($purchase_product['quantity']);
  $product_stock2=intval($product2['opening_stock'])+intval($data['quantity']);
  $product_insert1=array('opening_stock'=>$product_stock1,);
  $product_insert2=array('opening_stock'=>$product_stock2,);
  $this->db->update('product',$product_insert1,array('id'=>$purchase_product['id_product'],));
  $this->db->update('product',$product_insert2,array('id'=>$data['id_product'],));
  $this->db->update('purchase_product',$data,array('id'=>$id_pur_product,));
}
else if( $branch==0 AND $id_branch != 0)
{
 $warehouse1=$this->db->get_where('warehouse',array('id_product'=>$purchase_product['id_product']))->row_array();
   if(!empty( $warehouse1))
   {
   $warehouse_stock=intval($warehouse1['opening_stock'])-intval($purchase_product['quantity']);
    $insert1=array('opening_stock'=>$warehouse_stock,);
	 $this->db->update('warehouse',$insert1,array('id_product'=>$purchase_product['id_product'],));
	 } else{}
	  if($this->db->get_where('branch_product',array('id_product'=>$data['id_product'],'id_branch'=>$id_branch))->num_rows() !=0)
	 {
	 $branch=$this->db->get_where('branch_product',array('id_product'=>$data['id_product'],'id_branch'=>$id_branch,))->row_array();
   $branch_stock=intval($branch['opening_stock'])+intval($data['quantity']);
  $insert2=array('opening_stock'=>$branch_stock,);
  $this->db->update('branch_product',$insert2,array('id_product'=>$data['id_product'],'id_branch'=>$id_branch));
  } else{ $new=array('id_product'=>$data['id_product'],
                     'opening_stock'=>$data['quantity'],
					 'id_branch' =>$id_branch,
					 );
					 $this->db->insert('branch_product',$new);
  }
 $product1=$this->db->get_where('product',array('id'=>$purchase_product['id_product']))->row_array();
  $product2=$this->db->get_where('product',array('id'=>$data['id_product']))->row_array();
  $product_stock1=intval($product1['opening_stock'])-intval($purchase_product['quantity']);
  $product_stock2=intval($product2['opening_stock'])+intval($data['quantity']);
  $product_insert1=array('opening_stock'=>$product_stock1,);
  $product_insert2=array('opening_stock'=>$product_stock2,);
  $this->db->update('product',$product_insert1,array('id'=>$purchase_product['id_product'],));
  $this->db->update('product',$product_insert2,array('id'=>$data['id_product'],));
  $this->db->update('purchase_product',$data,array('id'=>$id_pur_product,));
$this->db->update('purchase_product',$data,array('id'=>$id_pur_product,));
}
else if ($branch !=0 AND $id_branch==0)
{
$branch1=$this->db->get_where('branch_product',array('id_product'=>$purchase_product['id_product'],'id_branch'=>$branch,))->row_array();
   if(!empty( $branch1))
   {
   $branch1_stock=intval($branch1['opening_stock'])-intval($purchase_product['quantity']);
    $insert1=array('opening_stock'=>$warehouse_stock,);
	 $this->db->update('branch_product',$insert1,array('id_product'=>$purchase_product['id_product'],'id_branch'=>$branch));
	 } else{}
  $product1=$this->db->get_where('product',array('id'=>$purchase_product['id_product']))->row_array();
  $product2=$this->db->get_where('product',array('id'=>$data['id_product']))->row_array();
  $product_stock1=intval($product1['opening_stock'])-intval($purchase_product['quantity']);
  $product_stock2=intval($product2['opening_stock'])+intval($data['quantity']);
  $product_insert1=array('opening_stock'=>$product_stock1,);
  $product_insert2=array('opening_stock'=>$product_stock2,);
  $this->db->update('product',$product_insert1,array('id'=>$purchase_product['id_product'],));
  $this->db->update('product',$product_insert2,array('id'=>$data['id_product'],));
 if($this->db->get_where('warehouse',array('id_product'=>$data['id_product']))->num_rows() !=0)
	 {
   $warehouse2=$this->db->get_where('warehouse',array('id_product'=>$data['id_product']))->row_array();
   $warehouse_stock2=intval($warehouse2['opening_stock'])+intval($data['quantity']);
  $insert2=array('opening_stock'=>$warehouse_stock2,);
  $this->db->update('warehouse',$insert2,array('id_product'=>$data['id_product'],));
  } else{ $new=array('id_product'=>$data['id_product'],
                     'opening_stock'=>$data['quantity'],);
					 $this->db->insert('warehouse',$new);
  }

$this->db->update('purchase_product',$data,array('id'=>$id_pur_product,));

}
else if($branch !=0 AND $id_branch !=0)
{
$branch1=$this->db->get_where('branch_product',array('id_product'=>$purchase_product['id_product'],'id_branch'=>$branch,))->row_array();
   if(!empty( $branch1))
   {
   $branch1_stock=intval($branch1['opening_stock'])-intval($purchase_product['quantity']);
    $insert1=array('opening_stock'=>$warehouse_stock,);
	 $this->db->update('branch_product',$insert1,array('id_product'=>$purchase_product['id_product'],'id_branch'=>$branch));
	 } else{}
  $product1=$this->db->get_where('product',array('id'=>$purchase_product['id_product']))->row_array();
  $product2=$this->db->get_where('product',array('id'=>$data['id_product']))->row_array();
  $product_stock1=intval($product1['opening_stock'])-intval($purchase_product['quantity']);
  $product_stock2=intval($product2['opening_stock'])+intval($data['quantity']);
  $product_insert1=array('opening_stock'=>$product_stock1,);
  $product_insert2=array('opening_stock'=>$product_stock2,);
  $this->db->update('product',$product_insert1,array('id'=>$purchase_product['id_product'],));
  $this->db->update('product',$product_insert2,array('id'=>$data['id_product'],));
 if($this->db->get_where('branch_product',array('id_product'=>$data['id_product'],'id_branch'=>$id_branch))->num_rows() !=0)
	 {
	 $branch=$this->db->get_where('branch_product',array('id_product'=>$data['id_product'],'id_branch'=>$id_branch,))->row_array();
   $branch_stock=intval($branch['opening_stock'])+intval($data['quantity']);
  $insert2=array('opening_stock'=>$branch_stock,);
  $this->db->update('branch_product',$insert2,array('id_product'=>$data['id_product'],'id_branch'=>$id_branch));
  } else{ $new=array('id_product'=>$data['id_product'],
                     'opening_stock'=>$data['quantity'],
					 'id_branch' =>$id_branch,
					 );
					 $this->db->insert('branch_product',$new);
  }

$this->db->update('purchase_product',$data,array('id'=>$id_pur_product,));

}

 }else{
if($id_branch==0)
{
 $product_new=$this->db->get_where('product',array('id'=>$data['id_product']))->row_array();

 $new_opening_stock=intval($product_new['opening_stock'])+intval($data['quantity']);
 $new_insert=array('opening_stock'=>$new_opening_stock,);
 $this->db->update('product',$new_insert,array('id'=>$data['id_product'],));
 $warehouse_new=$this->db->get_where('warehouse',array('id_product'=>$data['id_product']))->row_array();
 if(!empty($warehouse_new))
 {
  $warehouse_opnening_stock=intval($warehouse_new['opening_stock'])+intval($data['quantity']);
  $new_insert1=array('opening_stock'=>$warehouse_opening_stock,);
   $this->db->update('warehouse',$new_insert1,array('id_product'=>$data['id_product'],));
 }
else{
$new_insert2=array('id_product'=>$data['id_product'], 'opening_stock'=>$data['quantity'],);
$this->db->insert('warehouse',$new_insert2);
	}
$this->db->insert('purchase_product',$data);	

}
 else
 {
  $product_new=$this->db->get_where('product',array('id'=>$data['id_product']))->row_array();
 $new_opnening_stock=intval($product['opening_stock'])+intval($data['quantity']);
 $new_insert=array('opening_stock'=>$new_opening_stock,);
 $this->db->update('product',$new_insert,array('id'=>$data['id_product'],));
 $branch_new=$this->db->get_where('branch_product',array('id_product'=>$data['id_product'],'id_branch'=>$id_branch,))->row_array();
 if(!empty($branch_new))
 {
  $branch_opnening_stock=intval($branch_new['opening_stock'])+intval($data['quantity']);
  $new_insert1=array('opening_stock'=>$branch_opening_stock,);
   $this->db->update('branch_product',$new_insert1,array('id_product'=>$data['id_product'],'id_branch'=>$id_branch,));
 }
else{ 
$new_insert2=array('id_product'=>$data['id_product'], 'opening_stock'=>$data['quantity'],'id_branch'=>$id_branch);
$this->db->insert('branch_product',$new_insert2);
	}
	$this->db->insert('purchase_product',$data);	
 }
 }}
 }
////////////////////////
 /*public function update_item($data,$id_branch,$id_pur_product,$branch){ 
 if($id_branch==0 AND $branch==0)
 {
  $purchase= $this->db->get_where('purchase_product', array('id_product' => $data['id_product'],'id'=>$id_pur_product))->num_rows();
  if( $purchase !=0)
  {
 // $product =  $this->db->get_where("product",array( "id" => $new_data['id_product'] ))->row_array();
  //$warehous=  $this->db->get_where("warehouse",array( "id" => $new_data['id_product'] ))->row_array();
  
  }
  else
  {
  
  
  
  }
	
	}
}	*/
public function get_uniquepurchase($id){
	$query = $this->db->get_where(' purchase_product', array('id_purchase' => $id));
	return $query;
	
}	

public function  get_product_details($id){
	//$query = $this->db->get_where('product', array('code' => $code));
	//return $query;	
	
	$this->db->select('product.*,category.name AS category_name,company.name AS company_name' );
	$this->db->from('product', 'category','company','unit' );
	$this->db->join('category', 'product.id_category = category.id' );
	$this->db->join('company', 'product.id_company = company.id');
	$this->db->join('unit', 'product.id_unit = unit.id');
	$this->db->where(array('product.id' => $id));
	$q = 	$query = $this->db->get();
	$product = $q->row_array();
	$product_name = $product['code'].",".$product['name'].",".$product['category_name'].",".$product['company_name'];
	return  $product_name; 
}


public function delete($id){
		$this->db->delete('purchase',array('id'=>$id));
		$this->db->delete('purchase_product',array('id_purchase'=>$id));		
	}

public function edit_purchase($id){
	//echo $id;exit;
	$query = $this->db->get_where('purchase', array('id' => $id));
	return $query;
	
}

public function edit_products($id){
	
	$query = $this->db->get_where('purchase_product', array('id_purchase' => $id));
	return $query;

}

public function get_product_id($code){
	$query = $this->db->get_where('product', array('code' => $code))->row_array();
	
	   return $query['id'];
	
}
public function get_code($id){
    
	$query = $this->db->get_where('product', array('id' => $id))->row_array();
	
	   return $query['code'];
	
}
public function get(){
	   $this->datatables
		->select('purchase.id as id, purchase.referance_no,purchase.referance_date, purchase.received_date,supplier.name as supplier_name,purchase.amount, purchase.total_tax, purchase.total_disc,purchase.total_price,purchase.total_balance,purchase.transaction_type')
		->from('purchase','supplier' )
		->join('supplier', 'purchase.id_supplier= supplier.id'  )
		/*
			->select('product.id as id ,product.name,product.code, category.name AS category_name, company.name AS company_name,product.purchase_price,product.selling_price,product.opening_stock,product.alert_quantity')
			->from('product', 'company', 'category')
			->join('category', 'product.id_category= category.id')
			->join('company', 'product.id_company= company.id')
		*/
			->add_column("Actions", 
			"<center>			<a href='view_unique_purchase/$1' >
								<i class='icon-print'></i>
								</a>
								   <a href='edit_purchase/$1'>
								<i class='icon-edit'></i>
								</a>
							    </center>", "id")
			->unset_column('id');
		
		
	   return  $this->datatables->generate();
	
}

public function get_ref($id){
	
	$purchase= $this->db->get_where('purchase', array('id' => $id))->row_array();
	return $purchase;
	

	
}
function get_cname($id){
		$query = $this->db->get_where('supplier', array('id' => $id))->row_array();
		return $query['name'];
	
	
}

function get_unit($id){
		$this->db->select('unit.name AS unit_name' );
	$this->db->from('product','unit' );
	$this->db->join('unit', 'product.id_unit = unit.id');
	$this->db->where(array('product.id' => $id));
	$q = 	$query = $this->db->get();
	$product = $q->row_array();
	return  $product['unit_name']; 

}

public function get_excise(){
	$q = $this->db->get('excise_duty');
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
				
			return $data;
		}
		
	}
	
function supplier_details($id){
			$query = $this->db->get_where('supplier', array('id' => $id))->row_array();
			return $query;	
}	


}
